package com.aaa.static3;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Description:
 * @author:LiWei
 * @date:2022年11月09日19:49
 */
public class ProxyFactory implements MethodInterceptor {
    private Object target;

    public ProxyFactory(Object target) {
        this.target = target;
    }

    //获取代理类
    public Object getTarget(){
        Enhancer enhancer = new Enhancer();

        enhancer.setSuperclass(target.getClass());

        enhancer.setCallback(this);

        return enhancer.create();
    }



    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

        pre();

        Object invoke = method.invoke(target, objects);
        System.out.println(invoke);

        after();

        return invoke;
    }
    //前置校验内容
    public void pre(){
        System.out.println("校验内容");
    }

    //结束之后的日志内容
    public void after(){
        System.out.println("日志内容");
    }
}
